package com.example.my_helper;

import android.annotation.SuppressLint;
import android.bluetooth.BluetoothDevice;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import java.util.ArrayList;

public class BluetoothDeviceAdapter extends BaseAdapter {

    private ArrayList<MyBluetoothDevice> mData;
    private Context mContext;

    public BluetoothDeviceAdapter() {

    }


    public BluetoothDeviceAdapter(ArrayList<MyBluetoothDevice> mData, Context mContext) {
        this.mData = mData;
        this.mContext = mContext;
    }

    @Override
    public int getCount() {
        return mData.size();
    }

    @Override
    public Object getItem(int position) {
        return mData.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }


    @SuppressLint({"ViewHolder", "SetTextI18n"})
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;

        if(convertView != null){
            viewHolder = (ViewHolder)convertView.getTag();
        }else{
            viewHolder = new ViewHolder();
            convertView = LayoutInflater.from(mContext).inflate(R.layout.bluetooth_device_list_item, parent, false);
            viewHolder.device_name_label = convertView.findViewById(R.id.bluetooth_device_name_label);
            viewHolder.device_mac_label = convertView.findViewById(R.id.bluetooth_device_mac_label);
            viewHolder.device_rssi_label = convertView.findViewById(R.id.bluetooth_device_rssi_label);
            convertView.setTag(viewHolder);
        }

        viewHolder.device_name_label.setText(mData.get(position).device_name);
        viewHolder.device_mac_label.setText("mac："+mData.get(position).mac_address);
        viewHolder.device_rssi_label.setText("rssi："+mData.get(position).rssi);
        return convertView;
    }

    /** 创建 ViewHolder */
    class ViewHolder{
        TextView device_name_label;
        TextView device_mac_label;
        TextView device_rssi_label;
    }

}
